fix(ga): dedupe AI referral rows to winning attribution dimension#430
Merged
Conversation
GA4 emits one row per attribution dimension (session, first_user, manual_utm), but those are overlapping lenses on the same visit. The detail tables in /ga/traffic returned all three, inflating the row count (e.g. 1 source, 2 sessions, 6 rows). The chart, report, and aggregate counters already deduped — the tables were the missing case. Fix at the API so CLI, web, and MCP all see the same shape: keep the dimension with the highest sessions per (source, medium) and per (source, medium, landingPage). Adds a regression test that seeds multi-dimension rows and asserts only the winner is returned. Closes #428
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
GA4 emits one row per attribution dimension (
session,first_user,manual_utm) for the same visit, so/ga/trafficreturned three rows for what users perceive as one source — 1 source, 2 sessions, 6 rows in the bug report. The chart, report, and aggregate counters already deduped; the detail tables were the missing case. This collapses to the winning-dimension row per(source, medium)(and per(source, medium, landingPage)for the landing-page table) at the API so CLI, web, and MCP consumers all see the corrected shape. Adds a regression test that seeds three dimensions for the same source and asserts only the winner survives.Closes #428
Test plan
pnpm typecheckpassespnpm lintpassespnpm test— all 2090 tests pass, including the new dedup regression inpackages/api-routes/test/ga.test.ts